<%
' +----------------------------------------------------------------------
' | POPASP [ ASP MVC ]
' +----------------------------------------------------------------------
' | Copyright (c) 2016 http://popasp.com All rights reserved.
' +----------------------------------------------------------------------
' | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
' +----------------------------------------------------------------------
' | Author: popasp <1737025626@qq.com>
' +----------------------------------------------------------------------
Class POPASP_MODEL
	Private Function dateCompare( date1,date2 )
		dateCompare = DateDiff("s",date2,date1)
	End Function
	
	'创建一个Model文件
	Function CreateModelFile( db_type,db_path,tableName,prikey,ignoreExists )
		dim dirName,filePath,model_str,bool,modelPath
		CreateModelFile = False
		
		dirName = POP_MVC.File.baseName( array( db_path,1 ) )
		if dirName = "" Then dirName = "db"
		filePath = APP_PATH & "/Model/" & LCase(db_type) & "/" & dirName & "/" & POP_MVC.String.UCFirst( LCase(tableName) ) & "Model.class.asp"	'如./home/Model/excel/15251成绩/1学期Model.class.asp
		
		bool = false
		if not is_empty(ignoreExists) then	'忽略文件的存在性
			bool = true
		elseif POP_MVC.file.isExists( filePath ) then	'如果目标文件不存在
			bool = true
		end if
		
		if bool then
			'创建目标文件				
			model_str = POP_MVC.asp_get_contents( POP_MVC.mvc_dir & "Tpl/model2.html" )
			model_str = Replace(model_str,"TABLENAME" ,tableName ,1,1,0)
			model_str = Replace(model_str,"TABLENAME" ,tableName ,1,1,0)
			model_str = Replace(model_str,"PRIKEY" ,prikey ,1,1,0)
			call POP_MVC.file_put_contents( filePath, "<" & "%" & trim(model_str) & "%" & ">" )	
		end if
		
		if POP_MVC.file.isExists( filePath ) then	'如果目标文件不存在
			CreateModelFile = true
		end if
	End Function
	

	'创建Model对象，可以用 M_("表名") 来创建
	Function Create(ByVal tableName)
		on error resume next			
		Dim model_path,dst_path,model_str,bReload,key
		
		key = POP_MVC.getModelRunName(tableName) & "Model"	
		
		if POP_MVC.dModel.Exists(key) then
			if typeName( POP_MVC.dModel(key) ) = key Then
				set Create = POP_MVC.dModel(key)
				Exit Function
			end if
		end if
		
		bReload = false	
		
		model_path = POP_MVC.getModelPath( tableName )
		dst_path   = POP_MVC.getModelDstPath( tableName )	

		IF model_path <> "" Then
			If Not POP_MVC.file.isFile(dst_path) Then	'如果目标文件不存在					
				bReload = True
			ElseIf dateCompare(POP_MVC.file.mtime(POP_MVC.mvc_dir & "popasp_model.class.asp"),POP_MVC.file.mtime(dst_path)) > 0 Then	' 如果popasp_model.class.asp进行了最新修改				
				bReload = True
			ElseIf dateCompare(POP_MVC.file.mtime(model_path),POP_MVC.file.mtime(dst_path)) > 0 Then ' 如果 模型文件 进行了最新修改
				bReload = True			
			Else
				'获取文件内容
				bReload = false
				model_str = POP_MVC.asp_get_contents( dst_path )
			End If
			
			if bReload Then
				model_str = POP_MVC.asp_get_contents( model_path )				
				model_str = Replace(model_str,POP_MVC.UCFirst(tableName) & "Model" , POP_MVC.getModelRunName(tableName) & "Model" ,1,1,0)				
				model_str = Replace(model_str,"Model" ,"Model" & vbCrLf & vbTab & "Public db,that" ,1,1,0)				
				call POP_MVC.file_put_contents( dst_path, "<" & "%" & vbCrLf & trim(model_str) & vbCrLf & "%" & ">" )	
			End If			
		Else		
			If POP_MVC.file.isFile( dst_path )	Then '如果解析后的文件存在
				'获取文件内容
				model_str = POP_MVC.asp_get_contents( dst_path )
			Else
				'创建目标文件				
				model_str = POP_MVC.asp_get_contents( POP_MVC.mvc_dir & "Tpl/model1.html" )
				model_str = Replace(model_str,"TABLENAME" ,POP_MVC.getModelRunName(tableName) ,1,1,0)
				model_str = Replace(model_str,"TABLENAME" ,tableName ,1,1,0)
				call POP_MVC.file_put_contents( dst_path, "<" & "%" & vbCrLf & trim(model_str) & vbCrLf & "%" & ">" )	
			end If			
		End If
		
		

		
		'实例化类
		Execute model_str	

		
		
		if err.number <> 0 then
			if  is_empty(C_("APP_DEBUG")) then
				call L_("")				
			else 
				call L_( POP_MVC.UCFirst(tableName) & "Model 类出错：" )
				call POP_MVC.quit
			end if			
		end if
		
		Execute "set POP_MVC.dModel(""" & key & """) = new " & key
		
		
		
		'给对象中的db赋值
		set POP_MVC.dModel(key).db = P_("POPASP_" & UCase(C_("DB_TYPE")) )
		
		set POP_MVC.dModel(key).that =POP_MVC.dModel(key)
		
		

		if method_exists( tableName, "init" ) then
			Call POP_MVC.dModel(key).init
		end if
		
		set Create = POP_MVC.dModel(key)
		Call L_("")
	End Function	
End Class
%>